<?php

namespace App\Admin\Controllers\Company;

use App\Admin\Repositories\CompanyManagement;
use App\Models\Management;
use App\Models\Company;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ManagementController extends AdminController
{

    public $title = '公司管理员';

    /**
     * Make a grid builder.
     * 列表
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Management(), function (Grid $grid) {
            $grid->id->sortable();
            $grid->model()->with('company');
            $grid->column('name', '姓名');
            $grid->column('phone', '电话');
            $grid->column('username', '登录名');
            $grid->column('company', '管理公司')->display(function ($company) {
                return implode('，', array_column($company, 'name'));
            });
            $grid->column('status', '状态')->display(function($status){
                return Management::STATUS[$status];
            });
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->where('id', function ($query) {
                    $company_management = \App\Models\CompanyManagement::query()->where('company_id', $this->input)->pluck('management_id');
                    $query->whereIn('id', $company_management->all());
                }, '管理公司')->select(Company::query()->where('status', 1)->orderBy('id')->pluck('name', 'id'));
                $filter->like('name', '姓名');
                $filter->like('phone', '电话');
                $filter->equal('status', '状态')->select(\App\Models\Company::STATUS);

            });
        });
    }

    /**
     * Make a show builder.
     * 详情
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Management(), function (Show $show) use($id) {
            $show->id;
            $show->field('avatar', '头像')->image();
            $show->field('name', '姓名');
            $show->field('phone', '联系电话');
            $show->field('username', '账号');
            $show->field('company', '公司')->as(function ($company) use($id) {
                $management_info = Management::query()->find($id);
                $company = $management_info->company->toArray();
                return implode(',', array_column($company, 'name'));
            });
            $show->field('status', '状态')->as(function ($status){
                return Management::STATUS[$status];
            });
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     * 表单
     * @return Form
     */
    protected function form()
    {

        return Form::make(new Management(), function (Form $form){

            if($form->model()->id) {
                $select_company = \App\Models\CompanyManagement::query()->where('management_id', $form->model()->id)->get()->toArray();
                $form->model()->company = implode(',', array_column($select_company, 'company_id'));
            }

            $form->display('id');
            $form->image('avatar', '头像')->move('management/avatar', uniqid());
            $form->text('name','管理员姓名')->rules(function (Form $form){
                $rules = 'max:50|unique:management,name';
                $id = $form->model()->id;
                $rules .= ",{$id},id";
                $rules .= ',deleted_at,NULL';
                return $rules;
            });
            $form->text('phone', '联系电话')->rules('max:20');
            $form->multipleSelect('company', '管理公司')->options(Company::selectOption());
            $form->text('username', '登录名')->rules(function ($form) {
                $rules = 'required|max:50|unique:management,username';
                $id = $form->model()->id;
                $rules .= ",{$id},id";
                $rules .= ',deleted_at,NULL';

                return $rules;
            });
            $form->password('password', '登录密码')->default(123456)->rules('required');
            $form->switch('status', '状态')->default(1)->saving(function ($v) {
                return $v ? '1' : '0';
            });

            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }
            });

            $form->saved(function (Form $form) {
                $company_management_model = new CompanyManagement();
                $company_management_model->setData((int) $form->getKey(), (array) $form->company);
            });

        });
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * 删除
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DB::beginTransaction();
        $re_del_company = \App\Models\CompanyManagement::query()->where('management_id', $id)->delete();
        if($re_del_company === false) {
            return response()->json(['status'=> false, 'message'=> '服务器繁忙！']);
        }
        $re_del_management = Management::destroy($id);
        if(!$re_del_management) {
            DB::rollBack();
            return response()->json(['status'=> false, 'message'=> '服务器繁忙！']);
        }
        DB::commit();
        return response()->json(['status'=> true, 'message'=> '删除成功']);
    }
}
